package com.eling.elcms.system.service.impl;

import com.eling.elcms.system.service.ICaptchaService;
import nl.captcha.Captcha;
import nl.captcha.Captcha.Builder;
import nl.captcha.noise.CurvedLineNoiseProducer;
import nl.captcha.servlet.CaptchaServletUtil;
import nl.captcha.text.producer.DefaultTextProducer;
import nl.captcha.text.renderer.ColoredEdgesWordRenderer;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.util.*;
import java.util.List;

@Service
public class CaptchaServiceImpl implements ICaptchaService {

	/***
	 * 失效时间秒
	 */
	private static final int timeout = 120;
	private int _width = 200;
	private int _height = 50;
	private static final char[] letters = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'A', 'b',
			'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', 'h', 'H', 'k', 'K', 'm', 'M', 'n', 'N', 'p', 'P',
			'r', 'R', 'w', 'W', 'x', 'X', 'y', 'Y' };
	private static Map<String, String> codeMap = new HashMap<>();

	@Override
	public byte[] genCode(String id, String prefix) {

		Builder builder = new Captcha.Builder(_width, _height);
		// 增加边框
		builder.addBorder();

		// 干扰线
		CurvedLineNoiseProducer nosi = new CurvedLineNoiseProducer(Color.green, 5);
		builder.addNoise(nosi);

		builder.addNoise();
		// ----------------自定义字体大小-----------
		// 自定义设置字体颜色和大小 最简单的效果 多种字体随机显示
		List<Font> fontList = new ArrayList<Font>();
		fontList.add(new Font("Arial", Font.ITALIC, 40));// 可以设置斜体之类的
		// fontList.add(new Font("Courier", Font.BOLD, 40));
		/*
		 * List<Color> colorList = new ArrayList<Color>();
		 * colorList.add(Color.green); colorList.add(Color.BLUE);
		 * DefaultWordRenderer dwr=new DefaultWordRenderer(colorList,fontList);
		 */

		// 加入多种颜色后会随机显示 字体空心
		List<Color> colorList = new ArrayList<Color>();
		colorList.add(Color.green);
		// colorList.add(Color.white);
		colorList.add(Color.blue);
		ColoredEdgesWordRenderer cwr = new ColoredEdgesWordRenderer(colorList, fontList);
		builder.addText(new DefaultTextProducer(5, letters), cwr);
		// ---------装饰字体---------------
		// 字体边框齿轮效果 默认是3
		//builder.gimp(new BlockGimpyRenderer(1));
		// 波纹渲染 相当于加粗
		//builder.gimp(new RippleGimpyRenderer());
		// 修剪--一般不会用
		// builder.gimp(new ShearGimpyRenderer(Color.red));
		// 加网--第一个参数是横线颜色，第二个参数是竖线颜色
		// builder.gimp(new FishEyeGimpyRenderer(Color.red,Color.yellow));
		// 加入阴影效果 默认3，75
		// builder.gimp(new DropShadowGimpyRenderer());
		// 创建对象
		Captcha captcha = builder.build();
		String code = captcha.getAnswer();
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		CaptchaServletUtil.writeImage(bos, captcha.getImage());

		String key = id + "_" + prefix + "_" + System.currentTimeMillis();
		for (String ky : codeMap.keySet()) {
			if (ky.startsWith(id)) {
				codeMap.remove(ky);
				break;
			}
		}
		codeMap.put(key, code);
		return bos.toByteArray();
	}

	@Override
	public String validate(String id, String code, String prefixe) {
		String keyStr = null;
		for (String ky : codeMap.keySet()) {
			if (ky.startsWith(id + "_" + prefixe)) {
				keyStr = ky;
				break;
			}
		}
		//验证码不存在
		if (keyStr == null) {
			return "capthchaNotExist";
		}
		String[] keys = keyStr.split("_");
		Date genTime = new Date(Long.parseLong(keys[2]));
		Date nowTime = new Date();
		int min = (int) ((nowTime.getTime() - genTime.getTime()) / 1000);
		//验证码已失效
		if (min - timeout > 0) {
			return "captchaTimeout";
		}
		String answer = codeMap.get(keyStr);
		if (answer.equalsIgnoreCase(code)) {
			codeMap.remove(keyStr);
			return null;
		} else {
			//验证码不一致
			return "captchaNotMatch";
		}
	}

}
